package com.hckjsec.common.utils;
import com.alibaba.fastjson.JSON;
import com.hckjsec.common.config.DetectAuthLivingView;
import com.hckjsec.common.config.DetectAuthResultView;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthRequest;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthResponse;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoEnhancedRequest;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoEnhancedResponse;

import java.io.FileWriter;
import java.io.IOException;

public class DetectAuthUtil {
    private final static String  endpoint = "faceid.tencentcloudapi.com";

    //活体检测
    public static String getVideoResult(String secretId, String secretKey,String ruleId,String name,String idcord){
        String flag="";
        DetectAuthResponse resp=null;
        try{
            Credential cred = new Credential(secretId,secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            FaceidClient client = new FaceidClient(cred, "", clientProfile);
            DetectAuthRequest req = new DetectAuthRequest();
            req.setRuleId(ruleId);
            req.setName(name);
            req.setIdCard(idcord);
            resp = client.DetectAuth(req);
            flag=DetectAuthResponse.toJsonString(resp);
        }catch (TencentCloudSDKException e) {
            flag="";
            System.out.println(e.toString());
        }
        return flag;
    }
    //身份核验
    public static String getVerificationResult(String secretId, String secretKey,String ruleId){
        String flag="";
        DetectAuthResponse resp=null;
        try{
            Credential cred = new Credential(secretId,secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            FaceidClient client = new FaceidClient(cred, "", clientProfile);
            DetectAuthRequest req = new DetectAuthRequest();
            req.setRuleId(ruleId);
            resp = client.DetectAuth(req);
            flag=DetectAuthResponse.toJsonString(resp);
        }catch (TencentCloudSDKException e) {
            flag="";
            System.out.println(e.toString());
        }
        return flag;
    }

    public static DetectAuthResultView getVideoInfoRequest(String secretId, String secretKey,String region, DetectAuthLivingView param){
        Credential cred = new Credential(secretId,secretKey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(endpoint);
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        FaceidClient client = new FaceidClient(cred, region, clientProfile);
        return getResultHc(client,param);
    }

    public static DetectAuthResultView getResultHc(FaceidClient client, DetectAuthLivingView param){
        GetDetectInfoEnhancedResponse resp = null;
        try{
            GetDetectInfoEnhancedRequest req = new GetDetectInfoEnhancedRequest();
            req.setBizToken(param.getBizToken());
            req.setRuleId(param.getRuleId());
            req.setInfoType("0");
            req.setBestFramesCount((long) param.getBestFramesCount());
            req.setIsCutIdCardImage(true);
            req.setIsNeedIdCardAvatar(true);
            resp = client.GetDetectInfoEnhanced(req);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        String detectAuthResultStr = DetectAuthRequest.toJsonString(resp);
        //saveAsFileWriter(detectAuthResultStr);
        DetectAuthResultView detectAuthResult = JSON.parseObject(detectAuthResultStr, DetectAuthResultView.class);
        if(detectAuthResult.getVideoData() != null && !"{}".equals(detectAuthResult.getVideoData())) {
            detectAuthResult.setVideoData(JSON.parseObject(detectAuthResult.getVideoData()).get("LivenessVideo").toString());
        }
        return detectAuthResult;
    }

    public static void saveAsFileWriter(String content) {
        FileWriter fwriter = null;
        try {
            // true表示不覆盖原来的内容，而是加到文件的后面。若要覆盖原来的内容，直接省略这个参数就好
            fwriter = new FileWriter("E:\\test.txt", true);
            fwriter.write(content);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
